using UnityEngine;
using System.Collections;

public class sBullets : MonoBehaviour
{
	private static sBullets instance = null;

	private sBullets ()
	{
	}

	public static sBullets Instance
	{
		get
		{
			if (instance == null)
			{
				Debug.Log("instantiate bullet manager");
                GameObject go = new GameObject();
                instance = go.AddComponent<sBullets>();
                go.name = "sBullets";
			}
			return instance;
		}
	}
	
	public void shot(Vector3 vOrigin, Vector3 vDirection, Vector3 vUp, Vector2 vRandomDegrees)
	{
		Vector3 vRight = Vector3.Cross(vUp, vDirection);
		float fRandom = Random.Range(-vRandomDegrees.x, vRandomDegrees.x);
		vDirection.rotateAround(vUp, fRandom);
		fRandom = Random.Range(-vRandomDegrees.y, vRandomDegrees.y);
		vDirection.rotateAround(vRight, fRandom);
		
		// TODO this code should be done in the character class, because probably is useful for the pointing ik
		Camera oCamera = sCameras.Instance.getCamera();
		Ray r = oCamera.ScreenPointToRay( new Vector3( oCamera.pixelWidth * 0.5f, oCamera.pixelHeight * 0.75f, 0) );
		// if the ray from the camera hits something, update the destiny
		Vector3 vDestiny = r.origin + r.direction * 400.0f;
		if (sPhysics.Instance.linecastVsAll(r.origin, vDestiny))
		{
			vDestiny = sPhysics.Instance.getLinecastPoint();
			Debug.DrawRay(r.origin, r.direction * 10, Color.yellow);
		}
		
		if (sPhysics.Instance.linecastVsEnemies(vOrigin, vDestiny))
		{
			
		}
		else if (sPhysics.Instance.linecastVsAll(vOrigin, vDestiny))
		{
			sParticles.Instance.SpawnEffect("fxShotHit", sPhysics.Instance.getLinecastPoint(),
				Quaternion.LookRotation(sPhysics.Instance.getLinecastNormal()));
		}
	}
}
